(ns view.stranice
  (:use 
    [controllers.baza]
    [compojure.core]
    [hiccup.core] 
    [hiccup.page-helpers]   
    [ring.middleware.session]
    )
  (:require 
            [ net.cgrand.enlive-html :as htm]
            [clojure.java.io :as io]))

(defn view-layout [& content]
  (html
    (include-css "/stylesheets/main.css" )
    (include-js "/javascripts/application.js" )
    (doctype :xhtml-strict)
    (xhtml-tag "en"
      [:head
        [:meta {:http-equiv "Content-type"
                :content "text/html; charset=utf-8"}]
        [:title "Obrada slike"]
        ]
      [:body 
       [:div {:id "nasLogo"}
		     [:a {:href "/"}
		      [:img {:alt "login" :src "/stylesheets/Login.png"}]
		      ]
		     ]
       content
       [:div {:class "footer" :role "contentinfo"}]
     
       
       ])))


(defn view-registracija []
  (view-layout
    [:div {:class "body"} 
    [:h1 {:style "margin-left:2%"} "Registracija" ]
    ]
    [:div {:class "dialog"}
     
	    [:form {:method "post" :action "/registracija"}
      [:table
       [:tbody 
        [:tr {:class "prop"}
         [:td {:class "name"}
          [:label {:for "user"} "User:"]]
         [:td 
          [:input {:type "text" :id "user" :name "user"}]]
         ]
        [:tr {:class "prop"}
         [:td {:class "name"}
          [:label {:for "mail"} "Mail:"]]
         [:td 
          [:input {:type "text" :id "mail" :name "mail"}]]
         ]
        
        [:tr {:class "prop"}
         [:td {:class "password"}
          [:label {:for "password"} "Pass:"]]
         [:td 
          [:input {:type "password" :id "password" :name "password"}]]
         ]
        [:tr {:class "prop"}
         [:td {:class "password"}
          [:label {:for "password2"} "Confirm pass:"]]
         [:td 
          [:input {:type "password" :id "password2" :name "password2"}]]
         ]        
        ]
	     ]
      
      [:div {:class "buttons"}
       [:span {:class "button"}
        [:input {:style "margin-left:30%" :type "submit" :value "Register"}]]]
     ]
    ]
    
    )
  )

(defn view-login []
  (view-layout
    [:div {:class "body"} 
    [:h1 {:style "margin-left:2%"} "Login" ]
    ]
    [:div {:class "dialog"}
     
	    [:form {:method "post" :action "/login"}
      [:table
       [:tbody 
        [:tr {:class "prop"}
         [:td {:class "name"}
          [:label {:for "user"} "User:"]]
         [:td 
          [:input {:type "text" :id "user" :name "user"}]]
         ]
               
        [:tr {:class "prop"}
         [:td {:class "password"}
          [:label {:for "password"} "Pass:"]]
         [:td 
          [:input {:type "password" :id "password" :name "password"}]]
         ]
              
        ]
	     ]
      
      [:div {:class "buttons"}
       [:span {:class "button"}
        [:input {:style "margin-left:30%" :type "submit" :value "Login"}]]]
     ]
    ]
    
    )
  )

 
(defn postoji? [user]
  (
    let [pass (find-kor user)]
    pass
    )
  )

(defn reg
  [a sesija]
  
	  (let [kor (postoji? (a "user"))]
	  (if (= kor nil) 
       (view-layout 
       [:p](:generated_key (insert-user (a "user") (a "password") (a "mail")))
        )
	   (view-layout
	    [:h2 "Vec postoji korisnik sa tim user"]
	    [:p " " (if kor (str "postoji " (a "user")) (str "dobrodosli " (a "user"))  )]
	    [:a.action {:href "/"} "home"])
    )
	  )
  )

(defn login
  [a sesija]
  (def pas (find-kor (a "user"))  )  
  (:session (assoc sesija :user (str (a "user"))))
  (if (= pas (a "password"))        
    
      (view-layout
				    [:h2 "pronadjen"]
				    [:p "Dobrodosli " (str (a "user"))]
				    [:a.action {:href "/"} "neki link"])
         
          (view-layout
    [:h2 "nije pronadjen"]
    [:p "ne postoji takav korisnik" ]
    [:a.action {:href "/"} "neki link"])
         
         )
  
  )

(defn slika-d 
  [putanja]
  (prn putanja)
 {:status 200
       :headers {"Content-Type" "image/jpg"
                 "Content-Disposition"  (str "attachment;filename=" "slika.jpg") 
                
                 }
       :body (clojure.java.io/file putanja)
      }
  )
(defn view-output   
  []
  (view-layout
    [:h2 "Uspesna registracija"]
    [:p "neki text" ]
    [:a.action {:href "/"} "neki link"]))

(defn pocetna []
  (view-layout
    [:h2 "Obrada slike"]
    [:p "linkovi za reg i logovanje,najbolje sa slicicama"]
    [:a.action {:href "/"} "neki link"]))